package com.ddstation.home_page.service;

import com.ddstation.home_page.entity.DtInvsMessageQueueEntity;
import com.ddstation.home_page.mapper.DtInvsMessageQueueMapper;
import com.ddstation.home_page.model.CheckMessageModel;
import com.ddstation.home_page.model.HpCommonParModel;
import com.ddstation.home_page.model.MapModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class DtInvsMessageQueueServiceImpl implements DtInvsMessageQueueService {

	@Inject private DtInvsMessageQueueMapper dtInvsMessageQueueMapper;

	@Override
	public int insertMessageQueue(String institutionId,Integer businessSubType,Integer functionKey,Integer messageType,String targetId,Integer source) {
		DtInvsMessageQueueEntity entity = new DtInvsMessageQueueEntity();
		entity.setInstitutionId(institutionId);
		entity.setActionTm(new Date());
		entity.setBusinessSubType(businessSubType);
		entity.setFunctionKey(functionKey);
		entity.setMessageType(messageType);
		entity.setTargetId(targetId);
		entity.setSource(source);
		return dtInvsMessageQueueMapper.insert(entity);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public List<DtInvsMessageQueueEntity> getMessageQueue(String ua) {
		List<DtInvsMessageQueueEntity> list = new ArrayList<DtInvsMessageQueueEntity>();
		CheckMessageModel model = dtInvsMessageQueueMapper.checkMessage(ua);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.HOUR, -2);
		cal.getTime();
//		if (model.getMaxId() == 0 && model.getUpdateTm() == null) {
//			dtInvsMessageQueueMapper.insertMessageUa(ua);
//			return list;
//		}
//		else if (model.getUpdateTm().before(cal.getTime())) {	//时间超过2小时
//			dtInvsMessageQueueMapper.updateMessageUa(ua,model.getMaxId());
//			return list;
//		} else
//			if (model.getMaxId() == model.getMinId()) {	//无新记录
//			return list;
//		} else if (model.getMaxId() > model.getMinId()) {	//有新记录
//			list = dtInvsMessageQueueMapper.selectInvsMessageQueue(model.getMinId(),model.getMaxId());
//			dtInvsMessageQueueMapper.updateMessageUa(ua,model.getMaxId());
//			return list;
//		}
		return null;
	}

	@Override
	public List<MapModel> getMapModelByMessageQueues(HpCommonParModel _p,List<DtInvsMessageQueueEntity> list) {
//		if (list == null || list.size() == 0)
//			return new ArrayList<MapModel>();
		return dtInvsMessageQueueMapper.selectBadPracticingTown(_p.getInvsInstitutionId(),_p,list);
	}

}

